package acme.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import acme.entity.Department;

import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;

public class DepartmentDAOImpl extends SimpleDB implements CrudDao<Department>{
	private static final String DOMAIN = "Department";
	
	private static final String Department_ID = "DepartmentId";
	private static final String NAME = "Name";
	private static final String DESCRIPTION = "Description";
		
	public DepartmentDAOImpl() {
		super();
		initDomain(DOMAIN);
	}
	
	@Override
	public Class<Department> getClazz() {
		return Department.class;
	}

	@Override
	public Department createResource(Department dept) {
		PutAttributesRequest putAttributesRequest = new PutAttributesRequest();
		putAttributesRequest.setDomainName(DOMAIN);
		putAttributesRequest.setItemName(dept.getId());
		putAttributesRequest.setAttributes(this.store(dept));
		sdb.putAttributes(putAttributesRequest);
		return dept;
	}

	@Override
	public Collection<Department> getAllResources() {
		Collection<Department> collection = new ArrayList<Department>();
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN;
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		for(Item item : list){
			Department dept = this.load(item.getAttributes());
			collection.add(dept);
		}
		return collection;
	}

	@Override
	public Department readResource(String departmentId) {
		Department dept = null;
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN + " where " + Department_ID + " = '" + departmentId + "'";
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		if(list.size() > 0){
			Item item = list.get(0);
			dept = this.load(item.getAttributes());
		}
		return dept;
	}

	@Override
	public void updateResource(Department dept) {
		this.createResource(dept);
	}

	@Override
	public void deleteResource(String ticker) {
		DeleteAttributesRequest deleteAttributesRequest = new DeleteAttributesRequest(DOMAIN, ticker);
		sdb.deleteAttributes(deleteAttributesRequest);
	}

	private Collection<ReplaceableAttribute> store(Department dept){
		List<ReplaceableAttribute> list = new ArrayList<ReplaceableAttribute>();
		ReplaceableAttribute ra = new ReplaceableAttribute(Department_ID, dept.getId(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(NAME, dept.getName(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(DESCRIPTION, dept.getDescription(), true);
		list.add(ra);
		return list;
	}
	
	private Department load(List<Attribute> list){
		Department Department = new Department();
		for(Attribute ra : list){
			if(ra.getName().equals(Department_ID)){
				Department.setDepartmentId(ra.getValue());
				continue;
			}
			if(ra.getName().equals(NAME)){
				Department.setName(ra.getValue());
				continue;
			}
			if(ra.getName().equals(DESCRIPTION)){
				Department.setDescription(ra.getValue());
				continue;
			}
		}
		return Department;
	}
	
}
